﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Elderlyhealth.DAL;
public partial class _Default : System.Web.UI.Page
{
    private DataClasses1DataContext db = new DataClasses1DataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // 检查是否有未处理的警报
            int userId = Convert.ToInt32(Session["CustomerId"]);
            bool hasUnprocessedAlerts = CheckForUnprocessedAlerts(userId);

            // 如果有未处理的警报，调用弹窗方法
            if (hasUnprocessedAlerts)
            {
                ShowAlertPopup();
            }

            // 初始化数据绑定
            BindGrid();
        }
    }
    private bool CheckForUnprocessedAlerts(int userId)
    {
        // 在数据库中检查是否有未处理的警报
        return db.Talerts警报.Any(alert => alert.old_id == userId && alert.状态 == "待处理");
    }
    private void ShowAlertPopup()
    {
        string script = @"
        <script type='text/javascript'>
            window.onload = function() {
                if (confirm('您有未处理的异常警报！是否查看详情？')) {
                    window.location.href = 'AbAlerts.aspx'; // 跳转到异常警报页面
                }
            };
        </script>";
        ClientScript.RegisterStartupScript(this.GetType(), "AlertPopup", script);
    }
    private void BindElderInfo()
    {
        // 这里可以添加其他初始化数据的逻辑
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    private void BindGrid()
    {
        string searchText = txtSearch.Text.Trim();
        var elderData = db.Tolder老人.AsQueryable();

        // 如果搜索框有内容，则按条件筛选，否则显示所有数据
        if (!string.IsNullOrEmpty(searchText))
        {
            elderData = elderData.Where(e => e.old_name.Contains(searchText));
        }

        gvElderInfo.DataSource = elderData.ToList();
        gvElderInfo.DataBind();
    }



    protected void gvElderInfo_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedOldId = Convert.ToInt32(gvElderInfo.SelectedDataKey.Value);
        var healthData = db.Theath健康数据.Where(h => h.old_id == selectedOldId.ToString()).ToList();

        dvHealthData.DataSource = healthData;
        dvHealthData.DataBind();
        dvHealthData.Visible = true; // 显示 DetailsView
    }


    /*protected void dvHealthData_PageIndexChanging(object sender, DetailsViewPageEventArgs e)
    {
        dvHealthData.PageIndex = e.NewPageIndex;
        dvHealthData.DataBind();
    }*/
    protected void dvHealthData_PageIndexChanging1(object sender, DetailsViewPageEventArgs e)
    {
        dvHealthData.PageIndex = e.NewPageIndex;
        dvHealthData.DataBind();
    }
}